package com.yc.taixin.controller;

import com.alibaba.fastjson.JSONObject;
import com.yc.taixin.model.TSYuyue;
import com.yc.taixin.model.resultVo.AjaxJson;
import com.yc.taixin.model.resultVo.UsersVO;
import com.yc.taixin.service.impl.YuyueServiceImpl;
import com.yc.taixin.support.utils.NoImportUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 预约模块
 */
@RestController
@RequestMapping("/yuyue")
public class YuyueController extends BaseController {
	private static final Logger logger = LoggerFactory.getLogger(YuyueController.class);

	@Autowired
	private YuyueServiceImpl yuyueService;

	//预约数量统计单页
	@RequestMapping("/count")
	public ModelAndView countPage() {
		return new ModelAndView("yuyue/count");
	}

	//用户中心页
	@RequestMapping("/userInfo")
	public ModelAndView userInfoPage() {
		return new ModelAndView("yuyue/userInfo");
	}

	//预约预入口页
	@RequestMapping("/inletMain")
	public ModelAndView inletMainPage() {
		return new ModelAndView("yuyue/inletMain");
	}

	@GetMapping("/options")
	public ModelAndView yuyuePage() {
		return new ModelAndView("yuyue/options");
	}

	//获取/组装预约参数
	@RequestMapping("/getYuyueConf")
	public Object getYuyueConf(String restime) {
		return yuyueService.getYuyueConf(restime);
	}

	@RequestMapping("/saveYuyue")
	public AjaxJson saveYuyue(TSYuyue tsYuyue) {
		try {
			List<String> li = yuyueService.listYYtime();
			if (li.contains(tsYuyue.getRestime())) {
				return AjaxJson.errorMsg("该时间段已满员,请重新选择预约时间");
			}
			tsYuyue.setCreateDate(new Date());
			if (tsYuyue.getServerItem() != null && tsYuyue.getServerItem() > 0) {
				Map<Integer, String> map = NoImportUtil.serverItemMap_yc();
				String[] item = map.get(tsYuyue.getServerItem()).split("-");
				tsYuyue.setServerName(item[0]);
				tsYuyue.setPrice(item[1]);
			}
			tsYuyue.setPaystatus(0);
			tsYuyue.setProcess(0); //预约进程,如待受理
			String userId = this.getSessionUserId();
			if (userId != null && !"".equals(userId)) {
				tsYuyue.setUserId(userId);
			}
			Object p = yuyueService.update(tsYuyue);
			return AjaxJson.build(200, "您的预约申请我们已经收到,请等待客服确认.", "/yuyue/pay");
		} catch (NumberFormatException e) {
			e.printStackTrace();
			return AjaxJson.errorMap("预约失败,请稍候重试.");
		}
	}

	@RequestMapping("/pay")
	public ModelAndView payPage() {
		return new ModelAndView("yuyue/pay");
	}

	//用户预约清单
	@RequestMapping("/list")
	public ModelAndView listDataPage(Integer process) {
		Map<String, Object> map = new HashMap<>();
		if (process == null || process == -1) {
			process = -1;
		} else {
			map.put("action", "noAll");
		}
		map.put("process", process);
		map.put("userId", getSessionUserId());
		return new ModelAndView("yuyue/list").addObject("process", process)
				.addObject("count", yuyueService.countSuccess(map));
	}

	private String getSessionUserId() {
		UsersVO user = (UsersVO) request.getSession().getAttribute("user");
		return user == null ? "" : user.getId();
	}

	//预约成功列表
	@RequestMapping("/listSuccess")
	public Object listSuccess(Integer page, Integer limit, Integer process) {
		Map<String, Object> map = new HashMap<>();
		JSONObject object = new JSONObject();
		int start = 0;
		if (process != null && process != -1)
			map.put("process", process);
		if (limit != null)
			start = (page - 1) * limit;
		map.put("start", start);
		map.put("end", limit);
		map.put("userId", getSessionUserId());
		List<TSYuyue> list = yuyueService.listSuccess(map);
		object.put("list", list);
		return object;
	}

	//预约详细信息
	@RequestMapping("/details")
	public ModelAndView details(String yuyueId) {
		Map<String, Object> map = new HashMap<>();
		map.put("yuyueId", yuyueId);
		List<TSYuyue> list = yuyueService.listSuccess(map);
		return new ModelAndView("yuyue/details").addObject("details", list.get(0));
	}

}
